﻿using System;
using System.Windows.Forms;
using NUnit.Framework;
using vCrossPost.PingFM;
using Message=vCrossPost.PingFM.Message;

namespace vCrossPost.Test
{
    [TestFixture]
    public class NetworkTests
    {
        public const string API_KEY = "bc965dfba1c064d55bf3d782c1e4c09c";
        private const string USER_KEY = "efeecd240dc1b529b605aca4c90ac9e9-1234363640";

        private Api _api;

        private Api GetApi()
        {
            if (_api == null)
                _api = new Api(API_KEY, USER_KEY);

            return _api;
        }

        [Test]
        public void GetLatestMessages_Order_Test()
        {
            var api = GetApi();
            var msgs = api.GetLatestMessages(SortOrder.Ascending);

            Message previousMsg = null;
            foreach (var msg in msgs)
            {
                if (previousMsg != null)
                    Assert.GreaterOrEqual(msg.Date.Unix, previousMsg.Date.Unix);
                else
                    previousMsg = msg;
            }

            msgs = api.GetLatestMessages(SortOrder.Descending);

            previousMsg = null;
            foreach (var msg in msgs)
            {
                if (previousMsg != null)
                    Assert.LessOrEqual(msg.Date.Unix, previousMsg.Date.Unix);
                else
                    previousMsg = msg;
            }
        }

        [Test]
        public void GetLatestMessages_Test()
        {
            var api = GetApi();
            var msgs = api.GetLatestMessages();

            Assert.IsNotNull(msgs);
            CollectionAssert.IsNotEmpty(msgs);
        }

        [Test]
        public void GetSystemServices_Test()
        {
            var services = GetApi().GetSystemServices();

            Assert.IsNotNull(services);
            CollectionAssert.IsNotEmpty(services);
        }

        [Test]
        public void GetUserServices_Test()
        {
            var services = GetApi().GetUserServices();

            Assert.IsNotNull(services);
            CollectionAssert.IsNotEmpty(services);
        }

        [Test]
        public void Post_Test()
        {
            var msg = new PostMessage
            {
                Body = "Thử tiếng Việt",
                Title = "Thử tiếng Việt",
                Method = PostMethods.Blog,
                Debug = true
            };

            Assert.IsTrue(GetApi().Post(msg));
        }

        [Test]
        public void Post_With_Checksum_Test()
        {
            var msg = new PostMessage
            {
                Body = "Thử tiếng Việt",
                Title = "Thử tiếng Việt",
                Method = PostMethods.Blog,
                Debug = true
            };

            Assert.IsTrue(GetApi().Post(msg, true));
        }

        [Test]
        public void Validate_Test()
        {
            Assert.IsTrue(new Api(API_KEY, USER_KEY).Validate());
            Assert.IsFalse(new Api(API_KEY, "This is an invalid key").Validate());
        }
    }
}